<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a :class="[obj.sorter==null?'active':'']" @click="handle(null)" href="javascript:;">推荐</a>
      <a
        :class="[obj.sorter=='weight_desc'?'active':'']"
        @click="handle('weight_desc')"
        href="javascript:;"
      >最新</a>
      <div class="logo">
        <img alt src="@/assets/logo.png" />
      </div>
    </nav>

    <!-- <van-cell class="article-item">
      <template #title>
        <div class="head">
          <img
            alt
            src="http://teachoss.itheima.net/heimaQuestionMiniapp/%E5%AE%98%E6%96%B9%E9%BB%98%E8%AE%A4%E5%A4%B4%E5%83%8F%402x.png"
          />
          <div class="con">
            <p class="title van-ellipsis">宇宙头条校招前端面经</p>
            <p class="other">不风流怎样倜傥 | 2022-01-20 00-00-00</p>
          </div>
        </div>
      </template>
      <template #label>
        <div
          class="body van-multi-ellipsis--l2"
        >笔者读大三, 前端小白一枚, 正在准备春招, 人生第一次面试, 投了头条前端, 总共经历了四轮技术面试和一轮hr面, 不多说, 直接上题&nbsp;一面</div>
        <div class="foot">点赞 46 | 浏览 332</div>
      </template>
    </van-cell>-->

    <van-pull-refresh @refresh="onRefresh" v-model="refreshing">
      <van-list
        :finished="finished"
        @load="onLoad"
        finished-text="没有更多了"
        offset="50"
        style="padding-bottom:30px"
        v-model="loading"
      >
        <!--
.native  为什么需要添加
1、组件身上是没有标签元素提供的任何事件的====记住
2、如果写了有事件，这个只能是子组件传过来的  子组件  this.$emit('click')
3、如果想让组件也具备有标签元素事件  需要添加navite修饰符  ===作用：触发他的原生事件


        -->
        <!-- <ArticleItem :item="item" :key="item.id" @click.native="goDetail(item.id)" v-for="item in list"></ArticleItem> -->
        <!-- <ArticleItem :item="item" :key="item.id" @click="goDetail(item.id)" v-for="item in list"></ArticleItem> -->
        <ArticleItem :item="item" :key="item.id"  v-for="item in list"></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { ArticleAPI } from '@/api/article.js'
export default {
  name: 'article-page',
  data () {
    return {
      // pagenum: 0,
      // pagesize: 10,
      list: [],
      refreshing: false,
      loading: false,//上拉加载触发就会true,
      finished: false,//不能上拉了true  还能上拉 false
      obj: {
        current: 1,
        pageSize: 10,
        sorter: null//weight_desc
      }
    }
  },
  created () {
    // this.getArticleList()
  },
  methods: {
    handle (sorter) {
      this.obj.sorter = sorter
      this.refreshing = true
      this.onRefresh()
    },
    onLoad () {
      /* 上拉就会触发 */
      console.log('onLoad');
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      //   for (let i = 0; i < 10; i++) {
      //     this.list.push(this.list.length + 1);
      //   }

      //   // 加载状态结束
      //   this.loading = false;

      //   // 数据全部加载完成
      //   if (this.list.length >= 40) {
      //     this.finished = true;
      //   }
      // }, 1000);
      if (this.list.length >= 40) {
        this.finished = true;
        return
      }
      this.obj.current++
      this.getArticleList()


    },
    async getArticleList () {
      // const res = await ArticleAPI(this.obj).catch(err => err)
      // console.log(res.code, 66);
      // if (res.code == 10000) {
      //   this.list = res.data.rows
      // }

      try {
        const res = await ArticleAPI(this.obj)
        // this.list = res.data.rows
        this.list = this.refreshing ? res.data.rows : [...this.list, ...res.data.rows]
        this.loading = false;//关闭上拉加载
        this.refreshing = false
      } catch (error) {

      }

    },
    onRefresh () {
      // console.log('onRefresh');
      // // 清空列表数据
      // this.finished = false;

      // // 重新加载数据
      // // 将 loading 设置为 true，表示处于加载状态
      // this.loading = true;
      // this.onLoad();
      /* 数据获取第一页 */
      this.obj.current = 0
      this.loading = true
      this.onLoad()
    },

  }
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>